/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package avl.sv.client.source;



import avl.sv.server.slides.Slides;
import avl.sv.shared.SlideReference;
import avl.sv.shared.sources.SlideSource;
import avl.tiff.BufferingStatus;
import avl.tiff.TiffDirectoryBuffer;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.List;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.ws.WebServiceException;

/**
 *
 * @author benbryan
 */
public class SlideSourcePort extends SlideSource {
    private final Slides port;
    
    public SlideSourcePort(Slides port, SlideReference slideReference) {
        super(slideReference);
        this.port = port;
        gatherDirs();
    }

    private void gatherDirs() {
        try {
            List<String> ss = port.getImageInfo(getSlideReference().slideSetName, getSlideReference().slideName);
            for (int dirIdx = 0; dirIdx < ss.size(); dirIdx++) {
                String s = ss.get(dirIdx);
                Properties p = new Properties();
                ByteArrayInputStream is = new ByteArrayInputStream(s.getBytes());
                p.loadFromXML(is);
                TiffDirectoryBuffer tdb = new TiffDirectoryBuffer(p);
                byte tables[] = port.getJpegTables(getSlideReference().slideSetName, getSlideReference().slideName, dirIdx);
                if (tables != null) {
                    try {
                        tdb.setupDecoder(tables);
                    } catch (IOException ex) {
                        Logger.getLogger(SlideSourcePort.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
                getTiffDirectoryBuffers().add(tdb);
            }
        } catch (WebServiceException ex){
            try {
                Thread.sleep(1000);
            } catch (InterruptedException ex1) {
                Logger.getLogger(SlideSourcePort.class.getName()).log(Level.SEVERE, null, ex1);
            }
            gatherDirs();
            return;
        } catch (IOException ex) {
            Logger.getLogger(SlideSourcePort.class.getName()).log(Level.SEVERE, null, ex);
        }
        parseDBuff();
    }

    @Override
    protected BufferedImage downloadTile(TiffDirectoryBuffer dir, int i, int j) {
        BufferedImage img = null;
        try {
            byte b[];
            int dirIdx = getTiffDirectoryBuffers().indexOf(dir);
            b = port.getTile(getSlideReference().slideSetName, getSlideReference().slideName, dirIdx, i, j);
            if (b != null) {
                img = dir.setTile(i, j, b);
                dir.setTileFechingStatus(i, j, BufferingStatus.BUFFERED);
            } else {
                dir.setTileFechingStatus(i, j, BufferingStatus.NOT_BUFFERED);
            }
        } finally {
            decrementAndGetActiveDownloadCount();
            startBuffering();
        }
        return img;
    }
}
